<?php

namespace app\admin\controller;

use app\admin\model\Dismantlement;
use app\common\BaseHelper;
use think\Db;
use think\Exception;
use think\Request;

/**
 * 拆迁汇报
 */
class Dismantlementschedule extends Base
{

    public function _initialize(){
        parent::_initialize();

        $disId = $this->request->param('disId');
        $defaultWhere['disId'] = $disId;
        $this->defaultWhere = $defaultWhere;
        $this->redirect = url('admin/dismantlementschedule/index', ['disId' => $disId]);

        $this->assign('disId', $disId);
        // 拆迁户姓名
        $households = Dismantlement::where('id', $disId)->value('households');
        $this->assign('households', $households);
    }

    public function index(){
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();
            $model = model('DismantlementSchedule');
            // 每页起始条数
            $start = $param['start'];
            // 每页显示条数
            $length = $param['length'];
            // 排序条件
            $columns = $param['order'][0]['column'];
            $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                //$model = $model->where($this->defaultWhere);
                $where = array_merge($where, $this->defaultWhere);
            }

            $list = $model->where($where)->limit($start, $length)->order($order)->select();
            $count = $model->where($where)->count();

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count,
            ];

            return json($result);
        }
        return parent::index();
    }

    public function add(Request $request){

        return $this->fetch();
    }

    public function addPost(Request $request){
        if($request->isPost()){
            $param = $request->param();
            $model = model('DismantlementSchedule');

            try{
                $param['id'] = BaseHelper::getUUID();
                $time = $param['reportDate'] . ' ' . $param['reportDate_time'];
                $param['reportDate'] = $time;
                $result = $model->save($param);
                if($result != false){
                    return $this->success('添加成功！', $this->redirect);
                }else{
                    return $this->error('添加失败');
                }
            }catch (Exception $ex){
                return $this->error($ex->getMessage());
            }
        }else{
            return $this->error('非法请求');
        }
    }

    public function edit(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return $this->error('缺少参数');
        }

        $model = model('DismantlementSchedule');
        $info = $model->get($param['id']);
        $time = strtotime($info['reportDate']);
        $info['reportDate'] = date('Y-m-d',$time);
        $info['reportDate_time'] = date('H:i',$time);
        return $this->fetch('',[
            'info' => $info,
        ]);

    }
    public function editPost(Request $request){
        if($request->isPost()){
            $param = $request->param();
            $model = model('DismantlementSchedule');

            try{
                $result = $model->save($param, ['id' => $param['id']]);
                if($result != false){
                    return $this->success('修改成功！', $this->redirect,2);
                }else{
                    return $this->error('修改失败');
                }
            }catch (Exception $ex){
                return $this->error($ex->getMessage());
            }
        }else{
            return $this->error('非法请求');
        }
    }

    /**
     * 删除
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        $model = model('DismantlementSchedule');
        $where = [
            'id' => $id
        ];
        $result = $model->where($where)->delete();
        if($result !== false) {
            return $this->success('删除成功', 'admin/' . strtolower($this->model) . '/index');
        } else {
            return $this->error('删除失败 !');
        }
    }

}